package br.com.merks.vraptor.infra;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

import br.com.caelum.vraptor.Convert;
import br.com.caelum.vraptor.Converter;
import br.com.caelum.vraptor.converter.ConversionError;
import br.com.caelum.vraptor.ioc.ApplicationScoped;

 
@Convert(Date.class)
@ApplicationScoped
public class DateConverter implements Converter<Date> {

	public Date convert(String value, Class<? extends Date> type,
			ResourceBundle bundle) {
		if (value == null || value.equals("")) {
		    return null;
		}
	
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		try {
			return sdf.parse(value);
		} catch (ParseException e) {
			  throw new ConversionError(MessageFormat
				      .format(bundle.getString("is_not_a_valid_date"), value));
		}

	}

}
